package com.webwork.mood.record.dao.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.webwork.mood.common.dao.impl.SimpleDao;
import com.webwork.mood.record.dao.LevelScoreDao;
import com.webwork.mood.record.dataobject.Level;
import com.webwork.mood.record.dataobject.LevelScore;
import com.webwork.mood.util.ColumnLimit;
import com.webwork.mood.util.DBSimpleSQL;

@Repository
public class LevelScoreDaoImpl extends SimpleDao implements LevelScoreDao {

	public LevelScoreDaoImpl() {
		super();
		this.dbTable = "t_level_score";
		dbColumns.clear();
		dbColumns.put("score", "SCORE");
		dbColumns.put("level", "LEVEL");
	}
	
	@Override
	public ColumnLimit condition(Object object) {
		if (!(object instanceof LevelScore))
			return new ColumnLimit();
		LevelScore o = (LevelScore) object;
		ColumnLimit ret = new ColumnLimit();
		if (o.getScore() != 0) {
			ret.add(dbColumns.get("score") + " = ");
		}
		if (o.getLevel() != Level.NULL) {
			ret.add(dbColumns.get("level") + " = ");
		}
		return ret;
	}

	@Override
	public Object[] statement(Object object) {
		LevelScore o = (LevelScore) object;
		List<Object> ret = new ArrayList<Object>();
		if (o.getScore() != 0) {
			ret.add(o.getScore());
		}
		if (o.getLevel() != Level.NULL) {
			ret.add(o.getLevel().name());
		}
		return ret.toArray();
	}

	@Override
	public List<Object> pushObject(Object object) {
		LevelScore o = (LevelScore) object;
		List<Object> objs = new ArrayList<Object>();
		objs.add(o.getScore());
		objs.add(o.getLevel().name());
		return objs;
	}

	@Override
	public Object catchObject(Map<String, Object> map) {
		LevelScore ret = new LevelScore();
		ret.setScore((Integer) map.get(dbColumns.get("score")));
		ret.setLevel((String) map.get(dbColumns.get("level")));
		return ret;
	}
	
	@Override
	public void insert(LevelScore o) {
		String sql = DBSimpleSQL.insert(dbTable, dbColumns.size());
		this.getJdbcTemplate().update(sql, this.pushObject(o).toArray());
	}

	@Override
	public void update(LevelScore o) {
		ColumnLimit limit = new ColumnLimit();
		limit.add(dbColumns.get("score") + " = ");
		limit.add(dbColumns.get("level") + " = ");
		String sql = DBSimpleSQL.update(dbTable, dbColumns, limit);
		List<Object> objs = this.pushObject(o);
		objs.add(o.getScore());
		objs.add(o.getLevel().name());
		this.getJdbcTemplate().update(sql, objs.toArray());
	}

	@Override
	public void delete(LevelScore o) {
		ColumnLimit limit = new ColumnLimit();
		limit.add(dbColumns.get("score") + " = ");
		limit.add(dbColumns.get("level") + " = ");
		String sql = DBSimpleSQL.del(dbTable, limit);
		List<Object> objs = new ArrayList<Object>();
		objs.add(o.getScore());
		objs.add(o.getLevel().name());
		this.getJdbcTemplate().update(sql, objs.toArray());
	}

	@Override
	public List<LevelScore> query(LevelScore o, int page, int size) {
		List<LevelScore> ret = new ArrayList<LevelScore>();
		String sql = DBSimpleSQL.selectAll(dbTable, this.condition(o), page, size);
		List<Map<String, Object>> maps = this.getJdbcTemplate().queryForList(sql, this.statement(o));
		for (Map<String, Object> map : maps) {
			ret.add((LevelScore) this.catchObject(map));
		}
		return ret;
	}

	@Override
	public int count(LevelScore o) {
		String sql = DBSimpleSQL.count(dbTable, this.condition(o));
		return this.getJdbcTemplate().queryForObject(sql, this.statement(o), Integer.class);
	}

}
